package com.glela.controller;


import com.glela.domain.dto.GetRedPacketByShareKeyRequest;
import com.glela.domain.dto.GiveRedPacketByTemplateRequest;
import com.glela.domain.dto.ReceiveRedPacketRequest;
import com.glela.domain.dto.ShareRedPacketRequest;
import com.glela.domain.dto.SystemGiveRedPacketRequest;
import com.glela.domain.vo.GetRedPacketByShareKeyVo;
import com.glela.domain.vo.ShareRedPacketVo;
import com.glela.packet.exception.RedPacketBizException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.glela.biz.RedPacketBiz;
import com.glela.common.constant.ClientType;
import com.glela.common.constant.CommonConstants;
import com.glela.domain.dto.RedpacketRequset;
import com.glela.domain.vo.PerRedPacketResponseVo;
import com.glela.domain.vo.PerRedPacketVo;
import com.glela.platform.constant.ErrorEnum;
import com.glela.platform.model.User;
import com.glela.platform.model.response.ResponseBaseModel;
import com.glela.platform.model.response.ResponseResultModel;
import com.glela.platform.service.UserService;
import com.glela.platform.util.ParameterUtils;


@Controller
@RequestMapping(value = "/redPacket")
public class RedPacketController {
	
	private static Logger logger = LoggerFactory.getLogger(RedPacketController.class);

	@Autowired
	private UserService userService;
	
	@Autowired
	private RedPacketBiz redPacketBiz;
	

	/**
	 * 系统赠送红包-根据发行记录
	 */
	@ResponseBody
	@RequestMapping(value = "/systemGive", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> systemGive(@RequestBody SystemGiveRedPacketRequest request) {
		try {
			if (request == null || request.getRedPacketRecordId() <= 0 || CollectionUtils
					.isEmpty(request.getUserIdList())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			redPacketBiz.systemGiveRedPacket(request);
			return new ResponseEntity<>(new ResponseResultModel<>(ErrorEnum.C10000), HttpStatus.OK);
		} catch (RedPacketBizException e) {
			logger.error("系统赠送红包异常", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000, e.getMessage()), HttpStatus.OK);
		} catch (Exception e) {
			logger.error("系统赠送红包异常", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}


	/**
	 * 系统赠送红包-根据红包模板
	 */
	@ResponseBody
	@RequestMapping(value = "/giveByTemplate", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> giveRedPacketByTemplate(
			@RequestBody GiveRedPacketByTemplateRequest request) {
		try {
			if (request == null || request.getTemplateId() <= 0 || CollectionUtils
					.isEmpty(request.getUserIdList())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			redPacketBiz.giveRedPacketByTemplate(request);
			return new ResponseEntity<>(new ResponseResultModel<>(ErrorEnum.C10000), HttpStatus.OK);
		} catch (RedPacketBizException e) {
			logger.error("系统赠送红包异常", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000, e.getMessage()),
					HttpStatus.OK);
		} catch (Exception e) {
			logger.error("系统赠送红包异常", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}
	

	
	
	
	/**
	 * 
	* **********************************************
	* @Title: showRedPacketList 
	* @Description: 显示当前用户的红包列表
	* @param @param request
	* @param @return 
	* @return ResponseEntity<?>
	* @throws	
	* @author wy
	* @date 2018年3月23日 
	* **********************************************
	 */
	@SuppressWarnings("rawtypes")
	@ResponseBody
	@RequestMapping(value = "/showRedPacketList", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> showRedPacketListAA(@RequestBody  RedpacketRequset request,HttpServletRequest req) {
		try {
			if (ParameterUtils.validParams(request.getUserId()) && ParameterUtils.validParams(request.getType())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			User user = this.userService.getUserById(request.getUserId());
			if (null == user) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20113), HttpStatus.OK);
			}
			
			String clientType = ClientType.APP;
			if(req.getHeader("phoneType") != null && req.getHeader("phoneType").length() > 0) {
				if(req.getHeader("phoneType").toLowerCase().equals(CommonConstants.PHONE_CLIENT_IOS) || req.getHeader("phoneType").toLowerCase().equals(CommonConstants.PHONE_CLIENT_ANDROID)){
					clientType = ClientType.APP;
				} else if( req.getHeader("phoneType").toLowerCase().equals(CommonConstants.PHONE_CLIENT_h5) || req.getHeader("phoneType").toLowerCase().equals(CommonConstants.PHONE_CLIENT_WX)) {
					clientType = ClientType.H5;
				}
			}
			
			PerRedPacketResponseVo per = redPacketBiz.showRedPacketList(request.getUserId(),request.getType(),request.getStartIndex(),request.getPageSize(),clientType);
			
			return new ResponseEntity<>(new ResponseResultModel<>(per), HttpStatus.OK);
		} catch (Exception e) {
			logger.error("获取用户红包列表异常", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}

	/**
	 * 红包分享
	 */
	@SuppressWarnings("rawtypes")
	@ResponseBody
	@RequestMapping(value = "/shareRedPacket", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> shareRedPacket(@RequestBody ShareRedPacketRequest request) {
		if (request.getShareUserId() <= 0 || request.getRedPacketOwnerId() <= 0) {
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
		}
		try {
			ShareRedPacketVo shareRedPacket = redPacketBiz.shareRedPacket(request);
			return new ResponseEntity<>(new ResponseResultModel<>(shareRedPacket), HttpStatus.OK);
		} catch (RedPacketBizException e) {
			logger.error("用户分享红包异常", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000, e.getMessage()),
					HttpStatus.OK);
		} catch (Exception e) {
			logger.error("用户分享红包异常", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}

	/**
	 * 显示单个红包信息
	 */
	@SuppressWarnings("rawtypes")
	@ResponseBody
	@RequestMapping(value = "/redPacketInfo", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> redPacketInfo(@RequestBody GetRedPacketByShareKeyRequest request) {
		if (StringUtils.isBlank(request.getShareKey())) {
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
		}
		try {
			GetRedPacketByShareKeyVo vo = redPacketBiz.getRedPacketByShareKey(request);
			return new ResponseEntity<>(new ResponseResultModel<>(vo), HttpStatus.OK);
		} catch (RedPacketBizException e) {
			logger.error("用户分享红包异常", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000, e.getMessage()),
					HttpStatus.OK);
		} catch (Exception e) {
			logger.error("用户分享红包异常", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}


	/**
	 * 红包领取
	 */
	@SuppressWarnings("rawtypes")
	@ResponseBody
	@RequestMapping(value = "/receiveRedPacket", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> receiveRedPacket(@RequestBody ReceiveRedPacketRequest request) {
		if (StringUtils.isBlank(request.getShareKey()) || request.getReceiveUserId() <= 0) {
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
		}
		try {
			GetRedPacketByShareKeyVo vo = redPacketBiz.receiveRedPacket(request);
			return new ResponseEntity<>(new ResponseResultModel<>(vo), HttpStatus.OK);
		} catch (RedPacketBizException e) {
			logger.error("用户分享红包异常", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000, e.getMessage()),
					HttpStatus.OK);
		} catch (Exception e) {
			logger.error("用户分享红包异常", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}

}
